var express = require('express');
var router = express.Router();
var pool = require('../../config/mysqlconfig')
// http://localhost:3000/api/all
router.get("/all",function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log("数据库连接失败")
        }else{
            console.log("数据库连接成功")
            let sql = "select * from userinfo"
            conn.query(sql,function(error,results){
                if(error){
                    console.log("查询失败")
                }
                res.send(results)
                conn.release()
            })
        }
    })
})


// http://localhost:3000/api/add
router.post('/add',function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log("数据库连接失败")
        }else{
            let u_date = req.body.date;
            let u_name = req.body.name;
            let u_email = req.body.email
            let u_title = req.body.title;
            let u_evaluate = req.body.evaluate;
            let u_state = req.body.state;

            let data ={ 
                date:new Date(u_date),
                name:u_name,email:u_email,
                title:u_title,evaluate:u_evaluate,
                state:u_state
             }

             let sql = "insert into userinfo set ?"
             conn.query(sql,data,function(error,results){
                 if(error){
                     console.log(error)
                 }

                 if(results){
                    //  res.redirect('all')
                  
                    res.send(results)
                     conn.release()
                 }
             })

        }
    })
})
// http://localhost:3000/api/edit
router.put("/edit",function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log("数据库连接失败")
        }else{
            
            let u_date = req.body.date;
            let u_name = req.body.name;
            let u_email = req.body.email
            let u_title = req.body.title;
            let u_evaluate = req.body.evaluate;
            let u_state = req.body.state;
            let u_id = req.body.id;

            let data = [new Date(u_date),u_name,u_email,u_title,u_evaluate,u_state,u_id]

            let sql ='update userinfo set date=?,name=?,email=?,title=?,evaluate=?,state=? where id=?'
            conn.query(sql,data,function(error,results){
                if(error){
                    console.log("连接数据库失败")
                }else{
                    res.send(results)
                    conn.release()

                }
            })
        }
    })
})

router.delete("/del",function(req,res){
    pool.getConnection(function(err,conn){
        if(err){
            console.log("连接数据库失败")
        }else{
            let u_id = req.body.id
            let sql = "delete from userinfo where id = ?"

            conn.query(sql,u_id,function(error,results){
                if(error){
                    console.log(error)
                }
                if(results){
                    console.log(results)
                    res.send(results)
                    conn.release()
                }
            })
        }
    })
})

module.exports = router;